package com.dg.acb.utils.configdata;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import java.util.ArrayList;

public class DGConfigData {
    public static final Uri CONTENT_URI =
            Uri.parse( "content://com.xiaxiang.xiawan.config.data/datas");
    private static final String KEY_NAME = "name";	//配置名称
    private static final String KEY_VALUE = "value";//配置的值
    public  static final String KEY_ID = "_id";
    public  static final String TOKEN_KEY = "token";
    public  static final String USER_KEY = "user";
    public  static final String LOGIN_KEY = "login";
    public  static final String HEAD_KEY = "head";
    public final static String ICON_URL_KEY = "iconUrl";
    private Context context;
    public DGConfigData(Context context){
        this.context = context;
    }
    private final static String[] results_columns = new String[]{
            KEY_ID,
            KEY_NAME,
            KEY_VALUE,
            USER_KEY,
            TOKEN_KEY,
            HEAD_KEY,
            LOGIN_KEY
    };
    //更新配置值
    private boolean setConf( String name, String value ){
        ContentResolver cr = context.getContentResolver();

        //生成行数据
        ContentValues updateValues = new ContentValues();
        updateValues.put( KEY_VALUE, value );

        String where = KEY_NAME + "='" + name + "'";;
        String[] selectionArgs = null;
        int updateRowCount = cr.update(CONTENT_URI,
                updateValues, where, selectionArgs );


        if( updateRowCount > 0 ){
            return true;
        }else{
            return false;
        }
    }


    public boolean insertConf(String name,String value)
    {
        ContentResolver cr = context.getContentResolver();

        //生成行数据
        ContentValues insertValues = new ContentValues();
        insertValues.put( KEY_NAME, name );
        insertValues.put( KEY_VALUE, value );
        Uri url = cr.insert(CONTENT_URI, insertValues);
        if( url != null ){
            return true;
        }else{
            return false;
        }
    }
    public boolean updateConf(String name,String value)
    {
        boolean res = false;
        ContentResolver cr = context.getContentResolver();
        //生成参数
        String[] results_columns = new String[]{
                KEY_NAME,
                KEY_VALUE
        };
        String selection = KEY_NAME + "='" + name + "'";
        String[] selectionArgs = null;
        String sortOrder = null;

        //查询数据库
        Cursor cursor = cr.query( CONTENT_URI,
                results_columns, selection, selectionArgs, sortOrder);

        if(cursor != null){
            if( cursor.moveToFirst()){
                int columnIndex = cursor.getColumnIndexOrThrow( KEY_VALUE );
                //存在修改
                if (columnIndex > 0){
                    ContentValues updateValues = new ContentValues();
                    updateValues.put(KEY_VALUE, value);

                    int updateRowCount = cr.update(CONTENT_URI,
                            updateValues, selection, selectionArgs);
                    res = (updateRowCount > 0) ? true : false;
                }
            }

            //不存在，新建
            ContentValues insertValues = new ContentValues();
            insertValues.put( KEY_NAME, name );
            insertValues.put( KEY_VALUE, value );

            Uri url = cr.insert(CONTENT_URI, insertValues);
            res = (url != null ) ? true:false;
        }
        cursor.close();

        return res;
    }

    //获得配置
    public String getConf( String name ){
        ContentResolver cr = context.getContentResolver();

        //生成参数
        String[] results_columns = new String[]{
                KEY_NAME,
                KEY_VALUE
        };
        String selection = KEY_NAME + "='" + name + "'";
        String[] selectionArgs = null;
        String sortOrder = null;

        //查询数据库
        Cursor cursor = cr.query( CONTENT_URI,
                results_columns, selection, selectionArgs, sortOrder);

        //得到值后返回，默认值是空
        String value = null;
        if(cursor!=null){
            if( cursor.moveToFirst()){
                int columnIndex = cursor.getColumnIndexOrThrow( KEY_VALUE );
                value = cursor.getString( columnIndex );
            }
            cursor.close();
        }
        return value == null? "": value;
    }

    /*
     * 获取Token
     */
    public String getToken(){
        return getConf(TOKEN_KEY);
    }
    /*
     * 更新Token
     */
    public boolean updateToken(String token){
        return updateConf(TOKEN_KEY, token);
    }

    /*
     * 插入token
     */
    public boolean insertToken(String token){
        return insertConf(TOKEN_KEY, token);
    }

    /*
   * 获取Token
   */
    public String getUserName(){
        return getConf(USER_KEY);
    }

    public String getIconUrl(){
        return getConf(ICON_URL_KEY);
    }
    /*
     * 更新Token
     */
    public boolean updateUserName(String token){
        return updateConf(USER_KEY, token);
    }

    /*
     * 插入token
     */
    public boolean insertUserName(String token){
        return insertConf(USER_KEY, token);
    }

    private static synchronized ContentValues getContentValues(XXUserInfoData data){
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_ID,0);
        contentValues.put(KEY_NAME,"_name");
        contentValues.put(KEY_VALUE,"");
        contentValues.put(USER_KEY,data.mUser);
        contentValues.put(TOKEN_KEY, data.mToken);
        contentValues.put(HEAD_KEY, data.mHeadUrl);
        if(data.mIsLogin){
            contentValues.put(LOGIN_KEY,1);
        }else{
            contentValues.put(LOGIN_KEY,0);
        }

        return contentValues;
    }

    public static synchronized boolean insertUserInfoData(Context context,XXUserInfoData data)
    {
        ContentResolver cr = context.getContentResolver();
        //生成行数据
        ContentValues insertValues = getContentValues(data);
        Uri url = cr.insert(CONTENT_URI, insertValues);
        if( url != null ){
            return true;
        }else{
            return false;
        }
    }

    public static synchronized XXUserInfoData getUserInfo(Context context,Cursor cursor){
        if(cursor == null){
            return null;
        }
        XXUserInfoData data = null;
        int id = cursor.getInt(0);
        String name = cursor.getString(1);
        String value = cursor.getString(2);
        String userName = cursor.getString(3);
        String token = cursor.getString(4);
        String head = cursor.getString(5);
        int loginStyle = cursor.getInt(6);
        data = new XXUserInfoData();
        if(loginStyle == 1){
            data.mIsLogin = true;
        }else{
            data.mIsLogin = false;
        }
        data.mHeadUrl = head;
        data.mToken = token;
        data.mUser = userName;
        return data;
    }
    public static synchronized boolean deleteAllUserInfoData(Context context){
        ContentResolver cr = context.getContentResolver();
        String[] selectionArgs = null;
        int updateRowCount = cr.delete(CONTENT_URI, null, selectionArgs);
        if( updateRowCount > 0 ){
            return true;
        }else{
            return false;
        }
    }

    //获得配置
    public static synchronized ArrayList<XXUserInfoData> getAllUserInfoData(Context context){
        ContentResolver cr = context.getContentResolver();
        String[] selectionArgs = null;
        String sortOrder = null;

        //查询数据库
        Cursor cursor = cr.query( CONTENT_URI,
                results_columns, null, selectionArgs, sortOrder);
        int size = cursor.getColumnCount();
        if(size == 0){
            return null;
        }
        ArrayList<XXUserInfoData> dataList = new ArrayList<XXUserInfoData>(size);
        while (cursor.moveToNext()) {
            XXUserInfoData infoData = getUserInfo(context,cursor);
            dataList.add(infoData);
        }
        cursor.close();
        return dataList;
    }

    //更新配置值
    public static synchronized boolean deleteUserInfo(Context context,String user){
        ContentResolver cr = context.getContentResolver();
        String where = USER_KEY + "='" + user + "'";
        String[] selectionArgs = null;
        int updateRowCount = cr.delete(CONTENT_URI, where, selectionArgs);
        if( updateRowCount > 0 ){
            return true;
        }else{
            return false;
        }
    }

    public static synchronized XXUserInfoData getUserInfo(Context context,String user){
        XXUserInfoData data = null;
        String where = USER_KEY + "='" + user + "'";
        ContentResolver cr = context.getContentResolver();
        Cursor result = cr.query(CONTENT_URI, results_columns,
                where, null, null);
        if(result != null){
            int size = result.getColumnCount();
            while (result.moveToNext()) {
                data = getUserInfo(context,result);
                break;
            }
            result.close();
        }
        return data;
    }

    //更新配置值
    public static synchronized boolean updateUserInfo(Context context,String user,XXUserInfoData userInfoData) {
        ContentResolver cr = context.getContentResolver();
        if (getUserInfo(context, user) == null) {
            return false;
        }
        //生成行数据
        ContentValues updateValues = getContentValues(userInfoData);
        String where = USER_KEY + "='" + user + "'";
        String[] selectionArgs = null;
        int updateRowCount = cr.update(CONTENT_URI,
                updateValues, where, selectionArgs);

        if (updateRowCount > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean insertIconUrl(String token){
        return insertConf(ICON_URL_KEY, token);
    }
}

